package com.qdlc.p2p.manage.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.qdlc.p2p.biz.service.AccountCashService;
import com.qdlc.p2p.biz.service.AccountRechargeService;
import com.qdlc.p2p.biz.service.AccountService;
import com.qdlc.p2p.biz.service.BorrowService;
import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;

@SuppressWarnings("rawtypes")
public class OperatingInfoAction extends BaseAction {
	
	@Resource
	private UserService userService;
	@Resource
	private UserIdentifyService userIdentifyService;
	@Resource
	private BorrowService borrowService;
	@Resource
	private AccountRechargeService accountRechargeService;
	@Resource
	private BorrowTenderService borrowTenderService;
	@Resource
	private AccountCashService accountCashService;
	@Resource
	private AccountService accountService;
	
	/**
	 * 用户分析页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/operatingStatistics/userAnalysis")
	public String userAnalysis() throws Exception {
		return "userAnalysis";
	}

	/**
	 * 用户分析信息
	 * 
	 * @throws Exception
	 */
	@Action("/modules/operatingStatistics/userAnalysisInfo")
	public void userAnalysisInfo() throws Exception {
		// 转换JSON字符串用map
		Map<String, Object> data = new HashMap<String, Object>();
		
		// 成功注册用户总数
		QueryParam param = QueryParam.getInstance();
		int userCount = userService.count(param);
		data.put("userCount", userCount);
		
		// 实名认证成功用户数
		int userRealNamedCount = userIdentifyService.countByRealName(1);
		data.put("userRealNamedCount", userRealNamedCount);
		
		// 成功充值一次（含）以上的用户数
		int rechargedUserCount = accountRechargeService.rechargedUserCount();
		data.put("rechargedUserCount", rechargedUserCount);
		
		// 成功投资一次（含）以上的用户数
		int investUserCount = borrowTenderService.getInvestUserCount();
		data.put("investUserCount", investUserCount);
		
		// 已注册但未实名认证用户数
		int userRealNamelessCount = userIdentifyService.countByRealName(0);
		data.put("userRealNamelessCount", userRealNamelessCount);

		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 金额分析页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/operatingStatistics/momenyAnalysis")
	public String momenyAnalysis() throws Exception {
		return "momenyAnalysis";
	}

	/**
	 * 金额分析信息
	 * 
	 * @throws Exception
	 */
	@Action("/modules/operatingStatistics/momenyAnalysisInfo")
	public void momenyAnalysisInfo() throws Exception {
		// 转换JSON字符串用map
		Map<String, Object> data = new HashMap<String, Object>();
		
		// 累计充值金额总和
		double rechargedAllMomeny = accountRechargeService.rechargedAllMoney();
		data.put("rechargedAllMomeny", rechargedAllMomeny);
		
		// 投资人累计投资金额总和
		double tenderAccount = borrowTenderService.tenderAccount();
		data.put("tenderAccount", tenderAccount);
		
		// 投资人累计收到利息总和（实际收到）
		double tenderAllInterest = borrowTenderService.tenderAllInterest();
		data.put("tenderAllInterest", tenderAllInterest);
		
		// 累计提现金额总和
		double allCashMomeny = accountCashService.allCashMoney();
		data.put("allCashMomeny", allCashMomeny);
		
		// 投资人账户可用余额总和
		double allUseMoney = accountService.sumAllUseMoney();
		data.put("allUseMoney", allUseMoney);

		printJson(getStringOfJpaObj(data));
	}
	
	
	/**
	 * 项目数量分析页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/operatingStatistics/itemCountAnalysis")
	public String itemCountAnalysis() throws Exception {
		return "itemCountAnalysis";
	}

	
	
	/**
	 * 项目金额分析页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/operatingStatistics/itemMomenyAnalysis")
	public String itemMomenyAnalysis() throws Exception {
		return "itemMomenyAnalysis";
	}

	/**
	 * 项目金额分析信息
	 * 
	 * @throws Exception
	 */
	@Action("/modules/operatingStatistics/itemMomenyAnalysisInfo")
	public void itemMomenyAnalysisInfo() throws Exception {
		// 转换JSON字符串用map
		Map<String, Object> data = new HashMap<String, Object>();
		
		// 项目总金额
		double allMomeny = borrowService.getAllMomeny();
		data.put("allMomeny", allMomeny);
		
		// 逾期项目金额
		double allOverduedMomeny = borrowService.getAllOverduedMomeny();
		data.put("allOverduedMomeny", allOverduedMomeny);
		
		// 正常项目金额
		double allNormalMomeny  = BigDecimalUtil.sub(allMomeny, allOverduedMomeny);
		data.put("allNormalMomeny", allNormalMomeny);
		
		// 待登记项目金额
		/*double registerelessMomeny = borrowService.getMomenyByStatus(0);
		data.put("registerelessMomeny", registerelessMomeny);*/
		
		// 待初审项目金额
		double waitVerifyMomeny = borrowService.getMomenyByStatus(0);
		data.put("waitVerifyMomeny", waitVerifyMomeny);
		
		// 投资中项目金额
		double investingMomeny = borrowService.getMomenyByStatus(1);
		data.put("investingMomeny", investingMomeny);
		
		// 待满标复审项目金额
		double waitVerifyFullMomeny = borrowService.getVerifyFullMomeny();
		data.put("waitVerifyFullMomeny", waitVerifyFullMomeny);
		
		// 还款中项目金额
		double repayingMomeny6 = borrowService.getMomenyByStatus(6);
		double repayingMomeny7 = borrowService.getMomenyByStatus(7);
		double repayingMomeny = BigDecimalUtil.add(repayingMomeny6, repayingMomeny7);
		data.put("repayingMomeny", repayingMomeny);
		
		// 逾期中项目金额
		double allOverdueingMomeny = borrowService.getAllOverdueingMomeny();
		data.put("allOverdueingMomeny", allOverdueingMomeny);

		printJson(getStringOfJpaObj(data));
		
	}
	@Action("/modules/operatingStatistics/itemCountAnalysisInfo")
	public void itemCountAnalysisInfo() throws IOException{
		Map<String, Object> data = new HashMap<String, Object>();
		
		// 项目总条数
		double allCount = borrowService.getAllCount();
		data.put("allCount", allCount);
		
		// 逾期项目条数
		double allOverduedCount = borrowService.getAllOverduedCount();
		data.put("allOverduedCount", allOverduedCount);
		
		// 正常项目条数
		double allNormalCount  = BigDecimalUtil.sub(allCount, allOverduedCount);
		data.put("allNormalCount", allNormalCount);
		
		// 待初审项目条数
		double waitVerifyCount = borrowService.getCountByStatus(0);
		data.put("waitVerifyCount", waitVerifyCount);
		
		// 投资中项目条数
		double investingCount = borrowService.getCountByStatus(1);
		data.put("investingCount", investingCount);
		
		// 待满标复审项目条数
		double waitVerifyFullCount = borrowService.getVerifyFullCount();
		data.put("waitVerifyFullCount", waitVerifyFullCount);
		
		// 还款中项目条数
		double repayingCount6 = borrowService.getCountByStatus(6);
		double repayingCount7 = borrowService.getCountByStatus(7);
		double repayingCount = BigDecimalUtil.add(repayingCount6, repayingCount7);
		data.put("repayingCount", repayingCount);
		
		// 逾期中项目条数
		double allOverdueingCount = borrowService.getAllOverdueingCount();
		data.put("allOverdueingCount", allOverdueingCount);

		printJson(getStringOfJpaObj(data));
	}
}
